<?php
//this program will run every hour
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
set_time_limit(60*60);
ini_set('memory_limit', '512M');
date_default_timezone_set('America/New_York');

$g_start = microtime(true);
require_once "config.php";
require_once dirname(__FILE__)."/util/connection.class.php";
require_once dirname(__FILE__)."/util/redis.class.php";
$dbconn = Connection::getInstance();
$redis = RedisConnection::getRedis();

//$argsNum = count($argv);
//
//if($argsNum ==  2)
//{
//	$time = intval($argv[1]);
//}
//else
//{
	$now = time();
	$time = $now - $now % (60*60);// - 60*60;
//	$time = 1320908400;
//}
$dirname = RAW_DATA_ROOT.'/'. date('Y_m', $time);
$filename = $dirname.'/'.date('d_H',$time);
if (!file_exists($dirname))
{
	mkdir($dirname, 0600, true);
}
$file = fopen($filename, "a+");

$size = $redis->lSize('timeframe:'.$time);

if ($size == false)
{
	exit;
}

echo "# of hits: " . $size . "\n";

$sql = "select * from configs";
$result = $dbconn->query($sql);
$configs = array();
foreach ($result as $r)
{
	$configs[$r['k']] = $r['v'];
}
for ($i=0;$i<$size;$i++)
{
	$hitid = $redis->lPop('timeframe:'.$time);
	$hit = $redis->hGetAll($hitid);
	$redis->del($hitid);
	if (!isset($hit['event']))
	{
		print_r($hit);
	}
	if (!isset($hit['cam']))
	{
		$hit['cam'] = '';
	}
	if (!isset($hit['pub']))
	{
		$hit['pub'] = '';
	}
	if (!isset($hit['pla']))
	{
		$hit['pla'] = '';
	}
	if (!isset($hit['cre']))
	{
		$hit['cre'] = '';
	}
	if (!isset($hit['lu']))
	{
		$hit['lu'] = '';
	}
	if (!isset($hit['site']))
	{
		$hit['site'] = '';
	}
	if (!isset($hit['game']))
	{
		$hit['game'] = '';
	}
	if (!isset($hit['buy_type']))
	{
		$hit['buy_type'] = '';
	}
	if (!isset($hit['media_type']))
	{
		$hit['media_type'] = '';
	}
	if (!isset($hit['offer']))
	{
		$hit['offer'] = '';
	}
	if (!isset($hit['monetization']))
	{
		$hit['monetization'] = '';
	}
	if (!isset($hit['ip']))
	{
		$hit['ip'] = '';
	}
	if (!isset($hit['session']))
	{
		$hit['session'] = '';
	}
	if (!isset($hit['user']))
	{
		$hit['user'] = 0;
	}
	if (!isset($hit['won']))
	{
		$hit['won'] = '';
	}
	if (!isset($hit['credit']))
	{
		$hit['credit'] = '';
	}
	if (!isset($hit['bet']))
	{
		$hit['bet'] = '';
	}
	if (!isset($hit['account_credit']))
	{
		$hit['account_credit'] = '';
	}
	if(!isset($hit['account_balance']))
	{
		$hit['account_balance'] = '';
	}
	if (!isset($hit['token']))
	{
		$hit['token'] = '';
	}
	if (!isset($hit['revenue']))
	{
		$hit['revenue'] = '';
	}
	if (!isset($hit['share_email']))
	{
		$hit['share_email'] = '';
	}
	if (!isset($hit['share_facebook']))
	{
		$hit['share_facebook'] = '';
	}
	if (!isset($hit['share_twitter']))
	{
		$hit['share_twitter'] = '';
	}
	if (!isset($hit['offer_wall1']))
	{
		$hit['offer_wall1'] = '';
	}
	if (!isset($hit['offer_wall2']))
	{
		$hit['offer_wall2'] = '';
	}
	if (!isset($hit['offer_wall3']))
	{
		$hit['offer_wall3'] = '';
	}
	if (!isset($hit['offer_wall4']))
	{
		$hit['offer_wall4'] = '';
	}
	if (!isset($hit['bluebar_offer']))
	{
		$hit['bluebar_offer'] = '';
	}
	if (!isset($hit['banner_offer']))
	{
		$hit['banner_offer'] = '';
	}
	if (!isset($hit['dailydeals_offer']))
	{
		$hit['dailydeals_offer'] = '';
	}
	if (!isset($hit['email_type']))
	{
		$hit['email_type'] = '';
	}
	if (!isset($hit['email_offer_tosentdate']))
	{
		$hit['email_offer_tosentdate'] = '';
	}
	

	$key = 'tracking:var:cam:'.$hit['cam'].':cre:'.$hit['cre'].':lu:'.$hit['lu']. ':pub:'. $hit['pub']. ':pla:'. $hit['pla']. ':site:'. $hit['site'] . ':game:'. $hit['game'] . ':buy_type:' . $hit['buy_type']. ':media_type:'. $hit['media_type']. ':offer:'. $hit['offer']. ':monetization:'. $hit['monetization'].':bluebar_offer:'.$hit['bluebar_offer'].':email_type:'.$hit['email_type'].':email_offer_tosentdate:'.$hit['email_offer_tosentdate'].':banner_offer:'.$hit['banner_offer'].':dailydeals_offer:'.$hit['dailydeals_offer'];            
	if($hit['event'] == 'reward'){
		$key .= ':user:'.$hit['user'];
	}
	$redis->sAdd('tracking:var:all', $key);
	if (!empty($hit['cam']))
	{
		$redis->hSetNx($key, 'cam', $hit['cam']);
	}
	if (!empty($hit['cre']))
	{
		$redis->hSetNx($key, 'cre', $hit['cre']);
	}
	if (!empty($hit['lu']))
	{
		$redis->hSetNx($key, 'lu', $hit['lu']);
	}
	if (!empty($hit['user'])) {
		$redis->hSetNx($key, 'user', $hit['user']);
	}
	if (!empty($hit['pub']))
	{
		$redis->hSetNx($key, 'pub', $hit['pub']);
	}
	if (!empty($hit['pla']))
	{
		$redis->hSetNx($key, 'pla', $hit['pla']);
	}
	if (!empty($hit['site']))
	{
		$redis->hSetNx($key, 'site', $hit['site']);
	}
	if (!empty($hit['game']))
	{
		$redis->hSetNx($key, 'game', $hit['game']);
	}
	if (!empty($hit['monetization']))
	{
		$redis->hSetNx($key, 'monetization', $hit['monetization']);
	}
	if (!empty($hit['time']))
	{
		$redis->hSetNx($key, 'time', $hit['time']);
	}
	if (!empty($hit['offer_wall1']))
	{
		$redis->hSetNx($key, 'offer_wall1', $hit['offer_wall1']);
	}
	if (!empty($hit['offer_wall2']))
	{
		$redis->hSetNx($key, 'offer_wall2', $hit['offer_wall2']);
	}
	if (!empty($hit['offer_wall3']))
	{
		$redis->hSetNx($key, 'offer_wall3', $hit['offer_wall3']);
	}
	if (!empty($hit['offer_wall4']))
	{
		$redis->hSetNx($key, 'offer_wall4', $hit['offer_wall4']);
	}
	if (!empty($hit['bluebar_offer']))
	{
		$redis->hSetNx($key, 'bluebar_offer', $hit['bluebar_offer']);
	}
	if (!empty($hit['banner_offer']))
	{
		$redis->hSetNx($key, 'banner_offer', $hit['banner_offer']);
	}
	if (!empty($hit['dailydeals_offer']))
	{
		$redis->hSetNx($key, 'dailydeals_offer', $hit['dailydeals_offer']);
	}
	if (!empty($hit['email_type']))
	{
		$redis->hSetNx($key, 'email_type', $hit['email_type']);
	}
	if (!empty($hit['email_offer_tosentdate']))
	{
		$redis->hSetNx($key, 'email_offer_tosentdate', $hit['email_offer_tosentdate']);
	}
	$redis->hIncrBy($key, $hit['event'], 1);
	if ($hit['event'] == 'played')
	{
		$w = $redis->hGet($key, 'won');
		if (empty($w))
		{
			$w = 0;
		}
		$redis->hIncrBy($key, 'bet', $hit['bet']);
		$redis->hIncrBy($key, 'credit', $hit['credit']);
		$redis->hSet($key, 'won', $w+$hit['won']);
	}
	else if ($hit['event'] == 'reward')
	{
		$redis->hIncrBy($key, 'token', $hit['token']);
		$rev = $redis->hGet($key, 'revenue');
		if (empty($rev))
		{
			$rev = 0;
		}
		$redis->hSet($key, 'revenue', $rev+$hit['revenue']);
	}
	else if ($hit['event'] == 'basic_reg')
	{
		$redis->hIncrBy($key, 'credit', $hit['credit']);
	}
	else if ($hit['event'] == 'full_reg')
	{
		$redis->hIncrBy($key, 'credit', $hit['credit']);
	}
	else if ($hit['event'] == 'free')
	{
		$redis->hIncrBy($key, 'credit', $hit['credit']);
	}
	else if ($hit['event'] == 'basic_reg_coreg' || $hit['event'] == 'full_reg_coreg' || $hit['event'] == 'profiling_coreg_end' || $hit['event'] == 'confirmed')
	{
		if ($hit['monetization'] == 'Fluent')
		{
			$x = $configs['fluent_coreg_value'];
		}
		else if ($hit['monetization'] == 'Prospectiv')
		{
			$x = $configs['prospectiv_coreg_value'];
		}
		else
		{
			$x = 0;
		}
		$rev = $redis->hGet($key, 'revenue');
		if (empty($rev))
		{
			$rev = 0;
		}
		$redis->hSet($key, 'revenue', $x+$rev);
	}
	else if ($hit['event'] == 'ref_email')
	{
		$redis->hIncrBy($key, 'share_email', $hit['share_email']);
	}
	else if ($hit['event'] == 'ref_facebook')
	{
		$redis->hIncrBy($key, 'share_facebook', $hit['share_facebook']);
	}
	else if ($hit['event'] == 'ref_twitter')
	{
		$redis->hIncrBy($key, 'share_twitter', $hit['share_twitter']);
	}	
	
	$logKey = 'time>' . 'ip>' . 'session>' . 'user>' . 'event>' . 'bluebar_offer>' . 'email_type>' . 'email_offer_tosentdate>' . 'cam>' . 'pub>' 
	. 'pla>' . 'cre>' . 'lu>' . 'offer>' . 'site>' . 'game>' . 'buy_type>' . 'media_type>' . 'monetization>' . 'won>' . 'credit>' . 'bet>' 
	. 'token>' . 'revenue>' . 'share_email>' . 'share_facebook>' . 'share_twitter>' . 'offer_wall1>' . 'offer_wall2>' . 'offer_wall3>' . 'offer_wall4>'
	. 'account_credit>' . 'account_balance>' . 'banner_offer>' . 'dailydeals_offer';
	$logVal = $hit['time'].':'. $hit['ip']. ':' .$hit['session']. ':'. $hit['user']. ':' . $hit['event']. ':'. $hit['bluebar_offer']. ':'. $hit['email_type']
	. ':' . $hit['email_offer_tosentdate'] . ':'. $hit['cam']. ':' . $hit['pub'] . ':' . $hit['pla'] . ':' . $hit['cre']. ':'. $hit['lu']. ':'. $hit['offer']
	. ':'. $hit['site']. ':'. $hit['game']. ':' . $hit['buy_type'] . ':' . $hit['media_type']. ':'. $hit['monetization']. ':' . $hit['won'] . ':' . $hit['credit']
	.  ':' . $hit['bet'] . ':' . $hit['token'] . ':' . $hit['revenue'] . ':' . $hit['share_email'] . ':' . $hit['share_facebook'] . ':' . $hit['share_twitter']
	. ':' . $hit['offer_wall1'] . ':' . $hit['offer_wall2']  . ':' . $hit['offer_wall3'] . ':' . $hit['offer_wall4'] . ':' . $hit['account_credit']
	. ':' . $hit['account_balance'] . ':' . $hit['banner_offer'] . ':' . $hit['dailydeals_offer'];

	
	//$log = implode(':', $hit) . "\n";
	fwrite($file, $logVal . '-' . $logKey . "\n");
}
fclose($file);
$redis->del('timeframe:'.$time);

$dateTime = date('YmdHis', $time);
$count = 0;
$tbName = getTb($time, $dbconn); var_dump($tbName);
foreach ($redis->sMembers('tracking:var:all') as $key)
{
	$tracking = $redis->hGetAll($key);
	$impression = (isset($tracking['impression'])?$tracking['impression']:0);
	$user = isset($tracking['user']) ? $tracking['user'] : 0;
	$landing = (isset($tracking['landing'])?$tracking['landing']:0);
	$homeview = (isset($tracking['home_view'])?$tracking['home_view']:0);
	$conversion = isset($tracking['conversion'])?$tracking['conversion']:0;
	$suppress = isset($tracking['suppress'])?$tracking['suppress']:0;
	$morecredit = isset($tracking['more_credit'])?$tracking['more_credit']:0;
	$basic_reg = isset($tracking['basic_reg'])?$tracking['basic_reg']:0;
	$basic_reg_coreg = isset($tracking['basic_reg_coreg'])?$tracking['basic_reg_coreg']:0;
	$full_reg = isset($tracking['full_reg'])?$tracking['full_reg']:0;
	$full_reg_coreg = isset($tracking['full_reg_coreg'])?$tracking['full_reg_coreg']:0;
	$profiling_coreg_start = isset($tracking['profiling_coreg_start'])?$tracking['profiling_coreg_start']:0;
	$profiling_coreg_end = isset($tracking['profiling_coreg_end'])?$tracking['profiling_coreg_end']:0;
	$confirmed = isset($tracking['confirmed'])?$tracking['confirmed']:0;
	$confirmed_coreg = isset($tracking['confirmed_coreg'])?$tracking['confirmed_coreg']:0;
	$daily_coreg = isset($tracking['daily_coreg'])?$tracking['daily_coreg']:0;
	$played = isset($tracking['played'])?$tracking['played']:0;
	$bet = isset($tracking['bet'])?$tracking['bet']:0;
	$won = isset($tracking['won'])?$tracking['won']:0;
	$credit = isset($tracking['credit'])?$tracking['credit']:0;
	$reward = isset($tracking['reward'])?$tracking['reward']:0;
	$spend = isset($tracking['spend'])?$tracking['spend']:0;
	$revenue = isset($tracking['revenue'])?$tracking['revenue']:0;
	$token = isset($tracking['token'])?$tracking['token']:0;
	$free = isset($tracking['free'])?$tracking['free']:0;
	$offer = isset($tracking['offer'])?$tracking['offer']:'';
	$cam = isset($tracking['cam'])?$tracking['cam']:'';
	$cre = isset($tracking['cre'])?$tracking['cre']:'';
	$lu = isset($tracking['lu'])?$tracking['lu']:'';
	$pub = isset($tracking['pub'])?$tracking['pub']:'';
	$pla = isset($tracking['pla'])?$tracking['pla']:'';
	$game = isset($tracking['game'])?$tracking['game']:'';
	$site = isset($tracking['site'])?$tracking['site']:'';
	$offer_wall1 = isset($tracking['offer_wall1'])?$tracking['offer_wall1']:'';
	$offer_wall2 = isset($tracking['offer_wall2'])?$tracking['offer_wall2']:'';
	$offer_wall3 = isset($tracking['offer_wall3'])?$tracking['offer_wall3']:'';
	$offer_wall4 = isset($tracking['offer_wall4'])?$tracking['offer_wall4']:'';
	$monetization = isset($tracking['monetization'])?$tracking['monetization']:'';
	$buy_type = isset($tracking['buy_type'])? $tracking['buy_type']:'';
	$media_type = isset($tracking['media_type'])? $tracking['media_type']:'';
	$ref_email = isset($tracking['ref_email'])?$tracking['ref_email']:0;
	$ref_facebook = isset($tracking['ref_facebook'])?$tracking['ref_facebook']:0;
	$ref_twitter = isset($tracking['ref_twitter'])?$tracking['ref_twitter']:0;
	$share_email = isset($tracking['share_email'])?$tracking['share_email']:0;
	$share_facebook = isset($tracking['share_facebook'])?$tracking['share_facebook']:0;
	$share_twitter = isset($tracking['share_twitter'])?$tracking['share_twitter']:0;
	$ref_email_sent = isset($tracking['ref_email_sent'])?$tracking['ref_email_sent']:0;
	$ref_email_open = isset($tracking['ref_email_open'])?$tracking['ref_email_open']:0;
	
	$bluebar_offer = isset($tracking['bluebar_offer'])?$tracking['bluebar_offer']:'';
	$bluebar_click = isset($tracking['bluebar_click'])?$tracking['bluebar_click']:0;
	$bluebar_complete = isset($tracking['bluebar_complete'])?$tracking['bluebar_complete']:0;
	
	$banner_offer = isset($tracking['banner_offer'])?$tracking['banner_offer']:'';
	$banner_click = isset($tracking['banner_click'])?$tracking['banner_click']:0;
	$banner_complete = isset($tracking['banner_complete'])?$tracking['banner_complete']:0;
	
	$dailydeals_offer = isset($tracking['dailydeals_offer'])?$tracking['dailydeals_offer']:'';
	$dailydeals_click = isset($tracking['dailydeals_click'])?$tracking['dailydeals_click']:0;
	$dailydeals_complete = isset($tracking['dailydeals_complete'])?$tracking['dailydeals_complete']:0;
	
	$email_type = isset($tracking['email_type'])?$tracking['email_type']:'';
	$email_sent = isset($tracking['email_sent'])?$tracking['email_sent']:0;
	$email_open = isset($tracking['email_open'])?$tracking['email_open']:0;
	$email_click = isset($tracking['email_click'])?$tracking['email_click']:0;
	$email_offer_complete = isset($tracking['email_offer_complete'])?$tracking['email_offer_complete']:0;
	$email_offer_tosentdate = isset($tracking['email_offer_tosentdate'])?$tracking['email_offer_tosentdate']:0;
	
	$sql = "insert into {$tbName} set uid=?, time_new=?, cam=?, cre=?, lu=?, pub=?, pla=?, game=?, site=?, impression=?, landing=?, conversion=?, basic_reg=?, full_reg=?, confirmed=?, played=?, bet=?, won=?, credit=?, reward=?, offer=?, spend=?, revenue=?, homeview=?, suppress=?, morecredit=?, basic_reg_coreg=?, profiling_coreg_start=?, profiling_coreg_end=?, confirmed_coreg=?, daily_coreg=?, monetization=?, buy_type=?, media_type=?, token=?, free=?, full_reg_coreg=?, ref_email=?, ref_facebook=?, ref_twitter=?, share_email=?, share_facebook=?, share_twitter=?, ref_email_open=?, ref_email_sent=?, offer_wall1=?, offer_wall2=?, offer_wall3=?, offer_wall4=?, bluebar_offer=?, bluebar_click=?, bluebar_complete=?, banner_offer=?, banner_click=?, banner_complete=?, dailydeals_offer=?, dailydeals_click=?, dailydeals_complete=?, email_type=?, email_sent=?, email_open=?, email_click=?, email_offer_complete=?, email_offer_tosentdate=?";
	$ret = $dbconn->execute($sql, array($user, $dateTime, $cam, $cre, $lu, $pub, $pla, $game, $site, $impression, $landing, $conversion, $basic_reg, $full_reg, $confirmed, $played, $bet, $won, $credit, $reward, $offer, $spend, $revenue, $homeview, $suppress, $morecredit, $basic_reg_coreg, $profiling_coreg_start, $profiling_coreg_end, $confirmed_coreg, $daily_coreg, $monetization, $buy_type, $media_type, $token, $free, $full_reg_coreg, $ref_email, $ref_facebook, $ref_twitter, $share_email, $share_facebook, $share_twitter, $ref_email_open, $ref_email_sent, $offer_wall1, $offer_wall2, $offer_wall3, $offer_wall4, $bluebar_offer, $bluebar_click, $bluebar_complete, $banner_offer, $banner_click, $banner_complete, $dailydeals_offer, $dailydeals_click, $dailydeals_complete, $email_type, $email_sent, $email_open, $email_click, $email_offer_complete, $email_offer_tosentdate));
	if ($ret != 1)
	{
		echo $ret . "\n";
		exit;
	}
	$redis->del($key);
	$count++;
	if ($count % 10000 == 0)
	{
		echo "finished: ". $count. "\n";
	}
}
$redis->del('tracking:var:all');
//updateLastSiteDataIndexing($lastSiteDataTb, $count, $dateTime, $dbconn);

echo 'Totally: '. (microtime(true) - $g_start) . " seconds \n";
echo "Memory: " . memory_get_peak_usage() . " bytes \n";



function getTb($dateTime, Connection $dbconn)
{
	$tbName = 'site_data_' . date('Y_m', $dateTime);
	try{
		$sql = " CREATE TABLE IF NOT EXISTS $tbName LIKE site_data ";
		$result = $dbconn->execute($sql);
		if(!$result){
			return false;
		}
		return $tbName;
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}


/*
function getLastSiteDataTb(Connection $dbconn)
{
	$sql = " SELECT * FROM site_data_indexing ORDER BY id DESC LIMIT 1 ";
	try{
		$result = $dbconn->query($sql);
		if(!empty($result)){
			$lastSiteDataTb = $result[0];
		}else{
			$lastSiteDataTb = initSiteDataIndexing($dbconn);
		}
		
		return checkLastSiteDataTb($lastSiteDataTb, $dbconn);
		
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function initSiteDataIndexing(Connection $dbconn)
{
	$tbName = 'site_data';
	$sql = " SELECT COUNT(*) AS records_count, MAX(time_new) AS max_time_new FROM $tbName ";
	try{
		$result = $dbconn->query($sql);
		if(empty($result)){
			return false;
		}
		$result = $result[0];
		$recordsCount = $result['records_count'];
		$maxTimeNew = $result['max_time_new'];
		
		$sql = " INSERT INTO site_data_indexing SET tb_name=?, records_count=?, max_time_new=? ";
		if($dbconn->execute($sql, array($tbName, $recordsCount, $maxTimeNew))){
			return array(
				'id' => $dbconn->getLastInsert(),
				'tb_name' => $tbName,
				'records_count' => $recordsCount,
				'max_time_new' => $maxTimeNew
			);
		}
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}


function checkLastSiteDataTb($lastSiteDataTb, Connection $dbconn)
{
	$monthLastDay = mktime(23, 59, 59, date('m')+1, 0, date('Y'));
	if( strtotime($lastSiteDataTb['max_time_new']) > $monthLastDay){
		//is not this month
		$newTbName = 'site_data_' . date('Y_m', $monthLastDay);
		$sql = " CREATE TABLE $newTbName SELECT * from site_data where 1=2 ";
		if($dbconn->execute($sql)){
			$sql = " INSERT INTO site_data_indexing SET tb_name=? ";
			if($dbconn->execute($sql, array($newTbName))){
				return array(
					'tb_name' => $newTbName,
					'records_count' => 0,
					'max_time_new' => ''
				);
			}
			
			return false;
		}
	}
	
	//is this month
	return $lastSiteDataTb;
}

function updateLastSiteDataIndexing($lastSiteDataTb, $countInc, $maxTimeNew, Connection $dbconn)
{
	$sql = " UPDATE site_data_indexing SET records_count=?, max_time_new=? WHERE tb_name=? ";
	try{
		return $dbconn->execute($sql, array($lastSiteDataTb['records_count']+$countInc, $maxTimeNew, $lastSiteDataTb['tb_name']));
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' '.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}
*/


?>